package com.example.controller;

import com.example.entity.Role;
import com.example.entity.Teacher;
import com.example.service.TeacherService;
import com.example.utils.Msg;
import com.example.utils.PageUtil;
import com.example.utils.RestResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author： wzz
 * @date： 2021-10-19 15:54
 */

@Slf4j
@Controller
public class TeacherController {


    @Autowired
    private TeacherService teacherService;

    @RequestMapping("/back/queryTer")
    @ResponseBody
    public RestResponses queryAllTer(Integer page, Integer limit) {
        List<Teacher> teacherList = teacherService.queryAll();//3
//        List<Teacher> teacherList2 = teacherService.queryNoClass();//6
//
//        int flag = 0;
//
//        for (int i = 0; i < teacherList.size(); i++) {
//            for (int j = 0; j < teacherList2.size(); j++) {
//                if (teacherList.get(i).getNum().equals(teacherList2.get(j).getNum())) {
//                    flag = 1;
//                }
//            }
//            if (flag == 0) {
//                teacherList2.add(teacherList.get(i));
//            }
//            flag = 0;
//
//        }
//
        if (null != teacherList) {
            HashMap hashMap = PageUtil.PageByList(teacherList, page, limit, teacherList.size());
            return RestResponses.ok(hashMap);
        } else {
            return RestResponses.fail("查询出错");
        }
    }


    /**
     * 跳转老师信息编辑页面
     *
     * @return
     */
    @RequestMapping(value = "/back/toEditTeacher/{jobNumber}", method = RequestMethod.GET)
    public String toEditTeacher(@PathVariable("jobNumber") String jobNumber,
                                Model model) {

        Teacher teacher = teacherService.queryOne(jobNumber);
        model.addAttribute("teacher", teacher);
        model.addAttribute("allBooks",teacherService.getBooks());
        //查询该教师授课
        List<String> teachBooks = teacherService.getMyBooks(jobNumber);
        StringBuilder s = new StringBuilder();
        for (String teachBook : teachBooks) {
            s.append(teachBook);
            s.append(" ");
        }
        log.info("teachClasses：{}", s);
        model.addAttribute("teachBooks", s);

        return "back/UserManage/teacherEdit";
    }

    /**
     * 更新教师信息修改
     *
     * @return
     */
    @RequestMapping(value = "/back/updateTeacher")
    @ResponseBody
    public RestResponses updateTeacher(@RequestParam("jobNumber") String jobNumber,
                                       @RequestParam("name") String name,
                                       @RequestParam("phone") String phone,
                                       @RequestParam("trueBooks") String books) {

        log.info("jobNumber：{}", jobNumber);
        log.info("name：{}", name);
        log.info("phone：{}", phone);
        log.info("books：{}", books);
        int resC=0;
        int resT = teacherService.updateTeacher(jobNumber, name, phone);
        log.info("teacherInfo更新数据条数：{}", resT);
        teacherService.cancelTeacher(books);
        teacherService.cancelBooks(jobNumber);
        if(!books.equals("")){
            resC = teacherService.addBooks(jobNumber, books);
        }

        log.info("teacherBook更新数据条数：{}", resC);


        return RestResponses.ok();
    }

    /**
     * 删除老师信息
     *
     * @param jobNumber
     * @return
     */
    @RequestMapping("/back/delTeacher")
    @ResponseBody
    public RestResponses delTeacher(@RequestParam("jobNumber") String jobNumber) {
        int i = teacherService.delTeacher(jobNumber);
        if (i > 0) {
            log.info("删除成功");
            return RestResponses.ok();
        } else {
            log.info("删除失败");
            return RestResponses.fail();
        }
    }


    /**
     * 跳转至招聘计划添加页面
     *
     * @return
     */
    @RequestMapping(value = "/back/toAddTeacher", method = RequestMethod.GET)
    public String toAddTeacher() {
        return "/back/UserManage/teacherAdd";
    }

    /**
     * 添加老师信息
     *
     * @param teacher
     * @return
     */
    @RequestMapping("/back/AddTeacher")
    @ResponseBody
    public RestResponses addTeacher(Teacher teacher) {
        if (null == teacher.getDel()) {
            teacher.setDel(0);
        }
        int i = teacherService.addTeacher(teacher);
        if (i > 0) {
            log.info("添加成功");
            return RestResponses.ok();
        } else {
            log.info("添加失败");
            return RestResponses.fail();
        }
    }

    /**
     * 查找教师
     *
     * @param jobNumber
     * @param name
     * @return
     */
    @RequestMapping("/back/searchTeacher")
    @ResponseBody
    public RestResponses searchTeacher(@RequestParam("jobNumber") String jobNumber,
                                       @RequestParam("name") String name) {

        log.info("jobNumber：{}", jobNumber);
        log.info("name：{}", name);

        //获取教师的常规信息
        List<Teacher> teacherList = teacherService.searchTeacher(jobNumber, name);
        for(Teacher teacher:teacherList){
            Role role = new Role();
            role.setROLE_id(1);
            role.setROLE_name("ROLE_teacher");
            teacher.setRole(role);
        }
        log.info("teacherList：{}", teacherList);
        //获取老师的授课信息
        for (Teacher teacher : teacherList) {
            String teacherId = teacher.getNum();
            log.info("teacherId：{}", teacherId);
            List<String> classList = teacherService.getMyBooks(teacherId);
            log.info("classList：{}", classList);
            StringBuilder books = new StringBuilder();
            for (String s : classList) {
                books.append(s);
                books.append(" ");
            }
        }

        if (0 == teacherList.size()) {
            return RestResponses.fail("暂无数据");
        } else {
            HashMap hashMap = new HashMap();
            hashMap.put("content", teacherList);
            log.info("teacherList：{}", teacherList);
            return RestResponses.ok(hashMap);
        }
    }

    @GetMapping("/back/importToMyBook")
    @ResponseBody
    public Msg importToMy(@RequestParam(value = "classes")String classes,
                          @RequestParam(value = "book_name")String book_name){
        int result = teacherService.ImportToMyBook(classes,book_name);
        if(result!=0) return Msg.success();
        else return Msg.fail();
    }


}
